const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');

const multer=require('multer');
//设置上传路径
var multerObj=multer({dest:'./www/img'});
const fs=require('fs');
const pathLib=require('path')
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});
//========================
//业务逻辑
module.exports = function() {
    var router = express.Router();
//显示
    router.get('/updatnewest.html',function (req,res,next) {
        //判断是否登陆
        if(req.session['userId']==null||req.session['userId']=="")
        {
            res.status(400).send('<script>alert("请登录");window.location.href="login.html";</script>').end();
        }
        db.query("SELECT * FROM activityinfo,admininfo WHERE activityinfo.adminId=admininfo.adminId and activityinfo.activityid="+ +req.query.activityid+"",function (err,data) {
            if(err)
            {
                res.status(500).send('<script>alert("查询出错了!");window.location.href="404.html";</script>').end();
            }else
            {
                if(data.length==0)
                {
                    res.status(100).send('<script>alert("占时没有数据!");window.location.href="ertiaryGoods.html";</script>').end();
                }else
                {
                    req.activityid=data;

                    next();
                }
            }
        })

    });

//修改
    router.use('/updatnewest',function(req,res,next){

        if(req.files.length==0)
        {
            db.query("UPDATE activityinfo SET activityTitle='"+req.body.activityTitle+"', activitycontent='"+req.body.editorValue+"' WHERE activityid="+ +req.body.activityid+"",function (err,data) {
                if(err)
                {
                    console.log(err)
                    res.status(500).send('<script>alert("出错啦");</script>').end();
                }else
                {
                    res.status(400).send('<script>alert("修改成功");window.location.href="newest.html";</script>').end();
                    next();
                }
            });

        }else {
            //console.log("原名文件"+req.files[0].originalname);
            //获得文件扩展名
            //console.log("文件后缀"+pathLib.parse(req.files[0].originalname).ext);
            //文件全名
            var ext = pathLib.parse(req.files[0].originalname).ext;
            //console.log("dxcvbnm" + ext);
            //文件旧存放路径
            var oldPath = req.files[0].path;
            // console.log("文件旧存放路径"+oldPath)
            //文件新存放路径
            var newPath = req.files[0].path + ext;
            // console.log("文件新存放路径" + newPath)
            //文件新名
            var newFileName = req.files[0].filename + ext;

            fs.rename(oldPath, newPath, function (err) {
                if (err) {

                    res.status(500).send('<script>alert("出错啦");</script>').end();
                }
                else {
                    db.query("UPDATE activityinfo SET activityTitle='"+req.body.activityTitle+"', activitycontent='"+req.body.editorValue+"',activitypic='"+newFileName+"' WHERE activityid="+ +req.body.activityid+"",function (err,data) {
                        if(err)
                        {
                            res.status(500).send('<script>alert("出错啦");</script>').end();
                        }else
                        {
                            res.status(400).send('<script>alert("修改成功");window.location.href="newest.html";</script>').end();
                            next();
                        }
                    });

                }
            });
        }

    });
    router.get('/updatnewest.html',function(req,res){
        res.render("admin/updatnewest.ejs",{activityinfo:req.activityid});
    });
    return router;
}